package com.myBlast.server;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.myBlast.data.BlastResult;

public class BlastUtilitiies {
	
	public static boolean writeProject(ArrayList<BlastResult> blastResults, String fileNameWithoutExt)
	{
		boolean success = writeProjectSettings(blastResults, fileNameWithoutExt + ".prg");
		if (success) 
		{
			blastResults.remove(blastResults.size() - 1);
			success = writeCSV(blastResults, fileNameWithoutExt + ".csv");
			success = writeXLS(blastResults, fileNameWithoutExt + ".xls");
		}
		return success;
	}

	public static boolean writeProjectSettings(ArrayList<BlastResult> blastResults, String fileName)
	{
		boolean success = true;
		try
		{
			System.out.println("Writing file " + fileName);
			ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
			outputStream.writeObject(blastResults);
			outputStream.flush();
			outputStream.close();
		}
		catch(Exception e)
		{
			System.out.println("Exception while writing project settings");
			e.printStackTrace();
			success = false;
		}
		return success;
	}
	public static boolean writeCSV(List<BlastResult> rslts, String fileName)
	{
		boolean success = true;
		try
		{
			System.out.println("Writing csv file");
			FileWriter fw = new FileWriter(fileName);
			fw.write("organismName,organismId,maxEVal,minEVal,maxBitScore,minBitScore\n");
			for (BlastResult rslt : rslts)
			{
				fw.write(rslt.getOrganismName()+","+
						rslt.getOrganismId()+","+
						rslt.getMaxEVal()+","+
						rslt.getMinEVal()+","+
						rslt.getMaxBitScore()+","+
						rslt.getMinBitScore()+"\n");
			}
			fw.flush();
			fw.close();
			System.out.println("CSV file written");
		}
		catch(Exception e)
		{
			System.out.println("Exception while writting csv file");
			e.printStackTrace();
			success = false;
		}
		return success;
	}
	
	public static boolean writeXLS(List<BlastResult> rslts, String fileName)
	{
		boolean success = true;
		System.out.println("Writing xls file");
		try {
//		    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
			FileOutputStream fs = new FileOutputStream(fileName);
		    HSSFWorkbook wb = new HSSFWorkbook();
		    HSSFSheet sheet = wb.createSheet();
		    HSSFRow row = sheet.createRow(0);
		    HSSFCell cell;
		    
		    HSSFCellStyle cs1 = wb.createCellStyle( );
		    HSSFFont f1 = wb.createFont( );
		    f1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		    f1.setFontHeightInPoints((short)10);
		    cs1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		    cs1.setFont(f1);

		    for(int c = 0; c < 6; c++) {
                cell = row.createCell(c);
                cell.setCellStyle(cs1);
                if(cell != null) {
                	switch(c)
                    {
                    case 0:
                    	cell.setCellValue("Hit Name"); break;
                    case 1:
                    	cell.setCellValue("Organism Id"); break;
                    case 2:
                    	cell.setCellValue("Max E-Value"); break;
                    case 3:
                    	cell.setCellValue("Min E-Value"); break;
                    case 4:
                    	cell.setCellValue("Max Bit Score"); break;
                    case 5:
                    	cell.setCellValue("Min Bit Score");
                    }
                }
                if (c == 5) break;
            }
		    
		    
		    for(int r = 1; r <= rslts.size(); r++)
		    {
		        row = sheet.createRow(r);
		        if(row != null) 
		        {
		            for(int c = 0; c < 6; c++) 
		            {
		                cell = row.createCell(c);
		                if(cell != null) 
		                {
		                	BlastResult blastResult = rslts.get(r - 1);
		                	switch(c)
		                    {
		                    case 0:
		                    	cell.setCellValue(blastResult.getOrganismName()); break;
		                    case 1:
		                    	cell.setCellValue(blastResult.getOrganismId()); break;
		                    case 2:
		                    	cell.setCellValue(blastResult.getMaxEVal()); break;
		                    case 3:
		                    	cell.setCellValue(blastResult.getMinEVal()); break;
		                    case 4:
		                    	cell.setCellValue(blastResult.getMaxBitScore()); break;
		                    case 5:
		                    	cell.setCellValue(blastResult.getMinBitScore());
		                    }
		                }
		            }
		        }
		    }
		    wb.write(fs);
		    fs.close();
		} 
		catch(Exception ioe) 
		{
		    ioe.printStackTrace();
		    return false;
		}
		return success;
	}
}
